package com.tw.management.controller;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.tw.management.entity.User;
import com.tw.management.service.UserService;
import com.tw.management.utlis.PasswordAndSaltHelper;

/**
 * 
 * @author TangW
 * @version 1.0
 *
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

	private static Logger logger = LoggerFactory.getLogger(AdminController.class);

	@Resource(name = "userService")
	private UserService service;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String adminIndex() {
		logger.info(" Admin Page. ");
		return "admin/admin-index";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView listUser() {
		logger.info("/admin/list");
		ModelAndView mv = new ModelAndView();
		List<User> userList = service.listUsers();
		mv.addObject("userList", userList);
		mv.addObject("today", new Date());
		mv.setViewName("admin/admin-listUser");
		return mv;
	}

	@RequestMapping(value = "/save", method = RequestMethod.GET)
	public String userSave() {
		logger.info("/admin/save");
		return "admin/admin-saveUser";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String doUserSave(@ModelAttribute("user") User user) {
		logger.info("/admin/doSave - user=" + user.toString());
		Assert.hasText(user.getEmail(), "email can not be null");
		Assert.hasText(user.getPassword(), "password can not be null");
		Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		if (!pattern.matcher(user.getEmail()).matches()) {
			return "redirect:/register";
		}
		user.setUserType("1");
		user.setCreateDate(new Date());
		PasswordAndSaltHelper helper = new PasswordAndSaltHelper();
		helper.encryptPassword(user);
		user = service.saveUser(user);
		return "redirect:/admin/list";
	}

	@RequestMapping(value = "/update/{userId}", method = RequestMethod.GET)
	public ModelAndView updateUser(@PathVariable("userId") Long id) {
		logger.info("/admin/update - id=" + id);
		User userUpdate = service.findUserById(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("userUpdate", userUpdate);
		mv.setViewName("admin/admin-updateUser");
		return mv;
	}

	@RequestMapping(value = "/update/{userId}", method = RequestMethod.POST)
	public String doUpdateUser(@ModelAttribute("userUpdate") User user) {
		logger.info("/admin/doUpdate - user=" + user.toString());
		service.updateUserInfo(user);
		return "redirect:/admin/list";
	}

	@RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
	public String deleteUser(@PathVariable("userId") Long id) {
		logger.info("/admin/delete - id=" + id);
		service.deleteById(id);
		return "redirect:/admin/list";
	}

}
